package com.ruoyi.psi.sale.collection.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.psi.sale.collection.domain.CollectionList;
import com.ruoyi.psi.sale.collection.domain.SaleCollection;
import com.ruoyi.psi.sale.collection.service.ICollectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 销售收款
 * @author Drj 2021/6/28
 * 修改：lsy 2021.7.17
 */
@RestController
@RequestMapping("/psi/sale/collection")
@Api(value = "销售收款模块", tags = "销售收款接口")
public class CollectionController extends BaseController {
    @Autowired
    private ICollectionService collectionService;
    /**
     * 获取销售收款
     * @param collection
     * @return
     */
    @ApiOperationSupport(includeParameters = {"SaleCollection.collectionId", "SaleCollection.customerInfoId", "SaleCollection.collectionTime"})
    @ApiOperation(value = "获取销售收款列表",notes = "查询所有销售收款信息")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody SaleCollection collection) {
        startPage();

        List<SaleCollection> collections = collectionService.selectCollectionList(collection);
        return getDataTable(collections);
    }

    /**
     * 导出销售订单
     *
     * @param collection
     * @return
     */
    @PostMapping("/export")
    public AjaxResult export(@RequestBody SaleCollection collection) {
        List<SaleCollection> List = collectionService.selectCollectionList(collection);
        ExcelUtil<SaleCollection> util = new ExcelUtil<SaleCollection>(SaleCollection.class);
        return util.exportExcel(List, "销售订单单数据");
    }

    /**
     * 新增销售收款时查询指定客户对应的销售订单以及销售退货单据
     *
     * @param collection
     * @return
     */
    @PostMapping("/getCollectionBill")
    public AjaxResult getCollectionBill(@RequestBody SaleCollection collection) {
        List<CollectionList> List = collectionService.getCollectionBill(collection.getCustomerInfoId());
        return AjaxResult.success(List);
    }
    /**
     * 新增销售收款
     * @param collection 销售订单单据
     * @return
     */
    @ApiOperation(value = "新增销售收款", notes = "新增销售收款信息")
    @ApiImplicitParam(name = "collection", value = "新增销售收款(参数：collectionId，customerInfoId，collectionTime，discountAmount，remark)", dataType = "SaleCollection")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SaleCollection collection) {
        try {
            collectionService.insertCollection(collection);
            return AjaxResult.success("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新增失败");
        }
    }

    /**
     *
     * 更新销售订单
     * @param collection 销售订单单据
     * @return
     */
    @ApiOperation(value = "更新销售订单", notes = "更新销售订单")
    @ApiImplicitParam(name = "collection", value = "更新销售订单", dataType = "SaleCollection")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody SaleCollection collection) {
        try {
            collectionService.updateCollection(collection);
            return AjaxResult.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 删除销售订单
     * 仅修改删除标志位
     *
     * @param collection 销售订单单据
     * @return
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody SaleCollection collection) {

        return toAjax(collectionService.updateCollectionDeleteFlag(collection));
    }
}
